package com.rad.datastat.database;

import java.util.concurrent.locks.ReentrantLock;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "dataStatDB";

	private static final int DATABASE_VERSION = 1;

	private static DBOpenHelper sInstance;

	private final ReentrantLock mReentrantLock = new ReentrantLock(true);

	private SQLiteDatabase mDatabase;

	public ReentrantLock getLock() {
		return mReentrantLock;
	}

	public static synchronized DBOpenHelper instance(Context context) {
		if (sInstance == null) {
			sInstance = new DBOpenHelper(context.getApplicationContext());
		}
		return sInstance;
	}

	private DBOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public SQLiteDatabase getDatabase() {
		if (mDatabase == null || !mDatabase.isOpen()) {
			mDatabase = getWritableDatabase();
		}
		return mDatabase;
	}

	/**
	 * 在这里添加需要创建的数据表
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		creatStatisticDB(db);
	}
	
	private void creatStatisticDB(SQLiteDatabase db) {
		CallAndSmsDB.createTable(db);
		AppMd5DB.createTable(db);
		AppLaunchDB.createTable(db);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//nothing to do
	}

}
